home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c07 / controller / GreenhouseControls.java < prev   
Encoding:
Java Source  |  2000-05-25  |  6.0 KB  |  183 lines

  1. //: GreenhouseControls.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // This produces a specific application of the
  50. // control system, all in a single class. Inner
  51. // classes allow you to encapsulate different
  52. // functionality for each type of event.
  53. package c07.controller;
  54.  
  55. public class GreenhouseControls 
  56.     extends Controller {
  57.   private boolean light = false;
  58.   private boolean water = false;
  59.   private String thermostat = "Day";
  60.   private class LightOn extends Event {
  61.     public LightOn(long eventTime) {
  62.       super(eventTime);
  63.     }
  64.     public void action() {
  65.       // Put hardware control code here to 
  66.       // physically turn on the light.
  67.       light = true;
  68.     }
  69.     public String description() {
  70.       return "Light is on";
  71.     }
  72.   }
  73.   private class LightOff extends Event {
  74.     public LightOff(long eventTime) {
  75.       super(eventTime);
  76.     }
  77.     public void action() {
  78.       // Put hardware control code here to 
  79.       // physically turn off the light.
  80.       light = false;
  81.     }
  82.     public String description() {
  83.       return "Light is off";
  84.     }
  85.   }
  86.   private class WaterOn extends Event {
  87.     public WaterOn(long eventTime) {
  88.       super(eventTime);
  89.     }
  90.     public void action() {
  91.       // Put hardware control code here
  92.       water = true;
  93.     }
  94.     public String description() {
  95.       return "Greenhouse water is on";
  96.     }
  97.   }
  98.   private class WaterOff extends Event {
  99.     public WaterOff(long eventTime) {
  100.       super(eventTime);
  101.     }
  102.     public void action() {
  103.       // Put hardware control code here
  104.       water = false;
  105.     }
  106.     public String description() {
  107.       return "Greenhouse water is off";
  108.     }
  109.   }
  110.   private class ThermostatNight extends Event {
  111.     public ThermostatNight(long eventTime) {
  112.       super(eventTime);
  113.     }
  114.     public void action() {
  115.       // Put hardware control code here
  116.       thermostat = "Night";
  117.     }
  118.     public String description() {
  119.       return "Thermostat on night setting";
  120.     }
  121.   }
  122.   private class ThermostatDay extends Event {
  123.     public ThermostatDay(long eventTime) {
  124.       super(eventTime);
  125.     }
  126.     public void action() {
  127.       // Put hardware control code here
  128.       thermostat = "Day";
  129.     }
  130.     public String description() {
  131.       return "Thermostat on day setting";
  132.     }
  133.   }
  134.   // An example of an action() that inserts a 
  135.   // new one of itself into the event list:
  136.   private int rings;
  137.   private class Bell extends Event {
  138.     public Bell(long eventTime) {
  139.       super(eventTime);
  140.     }
  141.     public void action() {
  142.       // Ring bell every 2 seconds, rings times:
  143.       System.out.println("Bing!");
  144.       if(--rings > 0)
  145.         addEvent(new Bell(
  146.           System.currentTimeMillis() + 2000));
  147.     }
  148.     public String description() {
  149.       return "Ring bell";
  150.     }
  151.   }
  152.   private class Restart extends Event {
  153.     public Restart(long eventTime) {
  154.       super(eventTime);
  155.     }
  156.     public void action() {
  157.       long tm = System.currentTimeMillis();
  158.       // Instead of hard-wiring, you could parse
  159.       // configuration information from a text
  160.       // file here:
  161.       rings = 5;
  162.       addEvent(new ThermostatNight(tm));
  163.       addEvent(new LightOn(tm + 1000));
  164.       addEvent(new LightOff(tm + 2000));
  165.       addEvent(new WaterOn(tm + 3000));
  166.       addEvent(new WaterOff(tm + 8000));
  167.       addEvent(new Bell(tm + 9000));
  168.       addEvent(new ThermostatDay(tm + 10000));
  169.       // Can even add a Restart object!
  170.       addEvent(new Restart(tm + 20000));
  171.     }
  172.     public String description() {
  173.       return "Restarting system";
  174.     }
  175.   }
  176.   public static void main(String[] args) {
  177.     GreenhouseControls gc = 
  178.       new GreenhouseControls();
  179.     long tm = System.currentTimeMillis();
  180.     gc.addEvent(gc.new Restart(tm));
  181.     gc.run();
  182.   } 
  183. } ///:~